package com.summer.filesync.model;

import java.io.Serializable;
import java.util.Objects;

/** 节点信息 表示网络中的一个同步节点 */
public class Node implements Serializable {
  private static final long serialVersionUID = 1L;

  private String nodeId; // 节点唯一标识
  private String nodeName; // 节点名称
  private String hostAddress; // 主机地址
  private int port; // 端口号
  private NodeStatus status; // 节点状态
  private long lastSeen; // 最后活跃时间
  private String version; // 版本信息

  public enum NodeStatus {
    ONLINE, // 在线
    OFFLINE, // 离线
    CONNECTING, // 连接中
    SYNCHRONIZING, // 同步中
    ERROR // 错误状态
  }

  public Node() {
    this.lastSeen = System.currentTimeMillis();
    this.status = NodeStatus.OFFLINE;
  }

  public Node(String nodeId, String nodeName, String hostAddress, int port) {
    this();
    this.nodeId = nodeId;
    this.nodeName = nodeName;
    this.hostAddress = hostAddress;
    this.port = port;
  }

  /** 更新最后活跃时间 */
  public void updateLastSeen() {
    this.lastSeen = System.currentTimeMillis();
  }

  /** 检查节点是否在线 */
  public boolean isOnline() {
    return status == NodeStatus.ONLINE;
  }

  /** 获取节点地址字符串 */
  public String getAddressString() {
    return hostAddress + ":" + port;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null || getClass() != obj.getClass()) return false;
    Node node = (Node) obj;
    return Objects.equals(nodeId, node.nodeId);
  }

  @Override
  public int hashCode() {
    return Objects.hash(nodeId);
  }

  @Override
  public String toString() {
    return "Node{"
        + "nodeId='"
        + nodeId
        + '\''
        + ", nodeName='"
        + nodeName
        + '\''
        + ", hostAddress='"
        + hostAddress
        + '\''
        + ", port="
        + port
        + ", status="
        + status
        + ", lastSeen="
        + lastSeen
        + '}';
  }

  // Getters and Setters
  public String getNodeId() {
    return nodeId;
  }

  public void setNodeId(String nodeId) {
    this.nodeId = nodeId;
  }

  public String getNodeName() {
    return nodeName;
  }

  public void setNodeName(String nodeName) {
    this.nodeName = nodeName;
  }

  public String getHostAddress() {
    return hostAddress;
  }

  public void setHostAddress(String hostAddress) {
    this.hostAddress = hostAddress;
  }

  public int getPort() {
    return port;
  }

  public void setPort(int port) {
    this.port = port;
  }

  public NodeStatus getStatus() {
    return status;
  }

  public void setStatus(NodeStatus status) {
    this.status = status;
  }

  public long getLastSeen() {
    return lastSeen;
  }

  public void setLastSeen(long lastSeen) {
    this.lastSeen = lastSeen;
  }

  public String getVersion() {
    return version;
  }

  public void setVersion(String version) {
    this.version = version;
  }
}
